
package galaxy.app.network.rpc.handler;

import io.netty.channel.ChannelHandler.Sharable;
import galaxy.app.Galaxy;
import galaxy.app.dispatch.DispatcherSystem;
import galaxy.app.dispatch.executor.RequestPacketExecutor;
import galaxy.app.log.LogHelper;
import galaxy.app.network.rpc.packet.RequestPacket;
import galaxy.app.network.session.NettySession;
import galaxy.app.network.session.NodeSessionSystem;
import galaxy.app.route.RouteSystem;
import io.netty.channel.ChannelHandlerContext;


/**
 */
@Sharable
public class RequestHandler extends BaseChannelHandler<RequestPacket> {
    NodeSessionSystem nodeSession;
    RouteSystem route;

    DispatcherSystem dispatch;

    public RequestHandler() {
        super(RequestPacket.class);
        dispatch = Galaxy.systems().get(DispatcherSystem.class);
        nodeSession = Galaxy.systems().get(NodeSessionSystem.class);
        route = Galaxy.systems().get(RouteSystem.class);
    }

    @Override
    protected void onChannelRead(ChannelHandlerContext ctx, RequestPacket packet) {
        NettySession session = nodeSession.getSession(ctx);

        if (route.getRouteInfo(packet.route) == null) {
            LogHelper.warn("route not found. packet:{}", packet);
            return;
        }
        dispatch.publish(new RequestPacketExecutor( session, packet));
    }
}
